package com.opener.web.model.sys;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.opener.common.model.Model;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

/**
 *
 * @author baboy
 * @date 07/12/2016
 */
public class SysDict extends Model {

    public static class ValueType{
        public static String STRING = "String";
        public static String INT = "Integer";
        public static String LONG = "Long";
        public static String FLOAT = "Float";
        public static String LIST = "List";
        public static String MAP = "HashMap";
    }
    private static final long serialVersionUID = -7095108879458789231L;
    private Integer id;
    private String title;
    private String module;
    private String type;
    private String key;
    private String note;

    private Integer createUid;
    private Date createTime;
    private Integer updateUid;
    private Date updateTime;
    private List<SysDictItem> items;

    public static class SysDictItem extends Model{
        private static final long serialVersionUID = 4208300314227572060L;
        private Integer id;
        private Integer dictId;
        private String title;
        private String key;
        private String value;
        private String valueType;
        private String note;
        private Integer sortNo;
        private String createUid;
        private Date createTime;
        private Date updateUid;
        private Date updateTime;
        private Object metadata;

        public Integer getId() {
            return id;
        }

        public void setId(Integer id) {
            this.id = id;
        }

        public Integer getDictId() {
            return dictId;
        }

        public void setDictId(Integer dictId) {
            this.dictId = dictId;
        }

        public String getTitle() {
            return title;
        }

        public void setTitle(String title) {
            this.title = title;
        }

        public String getKey() {
            return key;
        }

        public void setKey(String key) {
            this.key = key;
        }

        public String getValue() {
            return value;
        }

        public void setValue(String value) {
            this.value = value;
        }

        public String getValueType() {
            return valueType;
        }

        public void setValueType(String valueType) {
            this.valueType = valueType;
        }

        public String getNote() {
            return note;
        }

        public void setNote(String note) {
            this.note = note;
        }

        public Integer getSortNo() {
            return sortNo;
        }

        public void setSortNo(Integer sortNo) {
            this.sortNo = sortNo;
        }

        public String getCreateUid() {
            return createUid;
        }

        public void setCreateUid(String createUid) {
            this.createUid = createUid;
        }

        public Date getCreateTime() {
            return createTime;
        }

        public void setCreateTime(Date createTime) {
            this.createTime = createTime;
        }

        public Date getUpdateUid() {
            return updateUid;
        }

        public void setUpdateUid(Date updateUid) {
            this.updateUid = updateUid;
        }

        public Date getUpdateTime() {
            return updateTime;
        }

        public void setUpdateTime(Date updateTime) {
            this.updateTime = updateTime;
        }

        public Object getMetadata() {
            return metadata;
        }

        public void setMetadata(Object metadata) {
            this.metadata = metadata;
        }
        public Object getTypedValue(){
            if (value == null){
                return null;
            }
            Object val = value;
            if (ValueType.INT.equalsIgnoreCase(valueType)){
                val = Integer.parseInt(value);
            }
            if (ValueType.LONG.equalsIgnoreCase(valueType)){
                val = Long.parseLong(value);
            }
            if (ValueType.FLOAT.equalsIgnoreCase(valueType)){
                val = Float.parseFloat(value);
            }
            try {
                if(value.startsWith("[") && ValueType.LIST.equalsIgnoreCase(valueType)){
                    ObjectMapper mapper = new ObjectMapper();
                    val = mapper.readValue(value, ArrayList.class);
                }else if(value.startsWith("{") && ValueType.MAP.equalsIgnoreCase(valueType)){
                    ObjectMapper mapper = new ObjectMapper();
                    val = mapper.readValue(value, HashMap.class);
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
            return val;
        }
    }

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public String getModule() {
        return module;
    }

    public void setModule(String module) {
        this.module = module;
    }

    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    public String getType() {
        return type;
    }

    public void setType(String type) {
        this.type = type;
    }

    public String getKey() {
        return key;
    }

    public void setKey(String key) {
        this.key = key;
    }

    public String getNote() {
        return note;
    }

    public void setNote(String note) {
        this.note = note;
    }

    public Integer getCreateUid() {
        return createUid;
    }

    public void setCreateUid(Integer createUid) {
        this.createUid = createUid;
    }

    public Date getCreateTime() {
        return createTime;
    }

    public void setCreateTime(Date createTime) {
        this.createTime = createTime;
    }

    public Integer getUpdateUid() {
        return updateUid;
    }

    public void setUpdateUid(Integer updateUid) {
        this.updateUid = updateUid;
    }

    public Date getUpdateTime() {
        return updateTime;
    }

    public void setUpdateTime(Date updateTime) {
        this.updateTime = updateTime;
    }

    public List<SysDictItem> getItems() {
        return items;
    }

    public void setItems(List<SysDictItem> items) {
        this.items = items;
    }

}
